package bmatch.tech.file.service.impl;

import bmatch.tech.file.model.MyDirectory;
import bmatch.tech.file.model.MyFile;
import bmatch.tech.file.model.Response;
import bmatch.tech.file.service.MyFileService;
import bmatch.tech.file.util.DataStore;
import bmatch.tech.file.util.IDUtil;
import bmatch.tech.file.util.Query;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * 文件服务实现类
 *
 * @author: Awake
 * @date: 2018-12-03 21:09
 **/
public class MyFileServiceImpl implements MyFileService {
    @Override
    public Response upload(InputStream inputStream, String userId, String path, String name) {
        //查询出是否已经存在
        if (Query.queryFile(userId, path, name) != null) {
            return new Response(Response.RESPONSE_OK, "文件已经存在");
        }

        String id = IDUtil.getUUID();
        File file = new File("./FilePool/" + id);
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream(file);
            int bytesWritten = 0;
            int byteCount = 0;
            byte[] bytes = new byte[1024];
            while ((byteCount = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, bytesWritten, byteCount);
                bytesWritten += byteCount;
            }
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //添加一个文件
        MyFile myfile = new MyFile();
        myfile.setId(id);
        myfile.setGmtCreated(new Date());
        myfile.setName(name);
        myfile.setPath(path);
        myfile.setStorePath(file.getAbsolutePath());
        DataStore.getFiles().get(userId).add(myfile);

        DataStore.storeFiles();
        return new Response(Response.RESPONSE_OK, "文件上传成功");
    }

    @Override
    public javax.ws.rs.core.Response downloadFile(String userId, String path, String name) {

        //查询出是否存在
        MyFile myFile =Query.queryFile(userId, path, name);
        if ( myFile == null) {
            Response resp = new Response();
            resp.setCode(Response.RESPONSE_BAD_REQUEST);
            resp.setMessage("文件不存在");
            return javax.ws.rs.core.Response.ok(resp, MediaType.APPLICATION_JSON).build();
        }

        File file = new File(myFile.getStorePath());
        return javax.ws.rs.core.Response
                .ok(file)
                .header("Content-disposition", "attachment;filename=" + myFile.getName())
                .header("Cache-Control", "no-cache").build();
    }

    @Override
    public Response delete(String userId, String path, String name) {
        //查询出文件夹是否存在
        if (Query.queryFile(userId, path, name) == null) {
            return new Response(Response.RESPONSE_OK, "文件不存在");
        }

        //按照所在路径列举所有文件 然后删除指定文件
        List<MyFile> myFiles = Query.queryFilesByUserId(userId);
        for (MyFile myFile : myFiles) {
            if (myFile.getPath().equals(path) && myFile.getName().equals(name)) {
                myFiles.remove(myFile);
                break;
            }
        }
        DataStore.storeFiles();
        return new Response(Response.RESPONSE_OK, "文件删除成功");

    }

    @Override
    public Response rename(String userId, String path, String originalName, String targetName) {
        //查询出文件夹是否存在
        MyFile myFile = Query.queryFile(userId, path, originalName);
        if (myFile == null) {
            return new Response(Response.RESPONSE_OK, "文件不存在");
        } else {
            myFile.setName(targetName);
            DataStore.storeFiles();
            return new Response(Response.RESPONSE_OK, "文件重命名成功");
        }
    }

    @Override
    public Response shift(String userId, String name, String originalPath, String targetPath) {
        //查询出文件夹是否存在
        MyFile myFile = Query.queryFile(userId, originalPath, name);
        if (myFile == null) {
            return new Response(Response.RESPONSE_OK, "文件不存在");
        } else {
            //修改路径即可
            myFile.setPath(targetPath);
            DataStore.storeFiles();
            return new Response(Response.RESPONSE_OK, "文件转移成功");
        }
    }


}
